package com.etc

import org.apache.spark.{SparkConf, SparkContext}

//累加变量
object Accumulator {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("Accumulator").setMaster("local")
    val sc = new SparkContext(conf)
    val sumAccumulator = sc.accumulator(0)
    val list = List(1,2,3,4,5,6,7,8,9)
    sc.parallelize(list).foreach(num => sumAccumulator += num)
    println(sumAccumulator)
    sc.stop()
  }
}
//广播变量
object BroadcastVariable{
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("BroadcastVariable").setMaster("local")
    val sc = new SparkContext(conf)
    val factor = 3
    val factorBroadcast = sc.broadcast(factor)
    val arr = Array(1, 2, 3, 4, 5)
    val rdd = sc.parallelize(arr)
    rdd.map(num => num * factorBroadcast.value).foreach(println)
    sc.stop()
  }

}